<?php
require_once '../../source/core/run.php';
require_once '../admin.inc.php';
$action		= Core_Fun::rec_post("action");
$page		= Core_Fun::detect_number(Core_Fun::rec_post("page"),1);

//$tpl->assign("mod",$mod);
//$tpl->display(ADMIN_TEMPLATE."frm_left.tpl");
 
 // list the days by month
 
switch($action){
    case 'edit';
	    edit();
		break;
	case 'saveedit';
	    saveedit();
		break;
	default:
	    volist();
		break;
}


function volist(){
	Core_Auth::checkauth("luck366volist");
	global $db,$tpl,$page;
	$sql = "SELECT * FROM `tc_luck366` WHERE `month` = ".$page;		         
	$days = $db->getall($sql);
	//var_dump($days);
	
	$tpl->assign("today", date("Y-m-d"));
	$tpl->assign("pagecount", 12);
	$tpl->assign("page", $page);
	$tpl->assign("listdata", $days);
}

function edit()
{
	Core_Auth::checkauth("luck366edit");
	global $db,$tpl;
    $id = Core_Fun::rec_post('id');
	
	if(!Core_Fun::isnumber($id)){
		Core_Fun::halt("ID丢失","",2);
	}
	$sql	= "SELECT * FROM `tc_luck366` WHERE uuid=$id";
	$cate	= $db->fetch_first($sql);
	if(!$cate){
		Core_Fun::halt("数据不存在","",2);
	}else{
	    $tpl->assign("id",$id);
	    $tpl->assign("cate",$cate);
		$tpl->assign("page",Core_Fun::rec_post('page'));
		$tpl->assign("comeurl","page=".Core_Fun::rec_post('page'));
	}
}

function saveedit()
{
	Core_Auth::checkauth("luck366add");
	global $db;
	$id			= Core_Fun::rec_post('id',1);
	$week		= Core_Fun::rec_post('week',1);
	$nongli		= Core_Fun::rec_post('nongli',1);
	$nonglid	= Core_Fun::rec_post('nonglid',1);
	$gangz_year	= Core_Fun::rec_post('gangz_year',1);
	$gangz_month	= Core_Fun::rec_post('gangz_month',1);
	$gangz_day	= Core_Fun::rec_post('gangz_day',1);
	$good	= Core_Fun::strip_post('good',1);
	$bad	= Core_Fun::strip_post('bad',1);
	$page = Core_Fun::rec_post('page',1);
	$content = Core_Fun::strip_post('content',1);
	
	$founderr	= false;
	if(!Core_Fun::isnumber($id)){
	    $founderr	= true;
		$errmsg	   .= "ID丢失.<br />";
	}
	if($founderr == true){
	    Core_Fun::halt($errmsg,"",1);
	}
	
	$array = array(
		'week'=>$week,
		'nongli'=>$nongli,
		'nonglid'=>$nonglid,
		'gangz_year'=>$gangz_year,
		'gangz_month'=>$gangz_month,
		'gangz_day'=>$gangz_day,
		'good'=>$good,
		'bad'=>$bad,
		'content'=>$content
	);
	//var_dump($array);
	$result = $db->update("`tc_luck366`",$array,"uuid=$id");
	if($result){
		Core_Command::runlog("","编辑运势365成功[id=$id]");
		Core_Fun::halt("编辑成功","luck365.php?page=".$page,0);
	}else{
		Core_Fun::halt("编辑失败","",2);
	}
}

$tpl->assign("action",$action);
$tpl->display("admin/yset/frm_list.tpl");
?>